var Tack = new Class({
	initialize: function (container) {
		this.html = {
			main: new Element('div', { 'class': 'tack' })/*,
			toolbox: new Element('div', { 'class': 'toolbox' }),
			select: new Element('ul', { 'class': 'select' })*/
		};

		/*
		this.toolboxPanes = {
			game: ['Game', new Tack.Pane.Game(this)],
			scene: ['Scene', new Tack.Pane.Scene(this)],
			debug: ['Debug', new Tack.Pane.Debug(this)]
		};
		*/

		this.designer = new Tack.Designer(this);
		this.player = new Tack.Player(this.game);
		this.sidebar = new Tack.Sidebar(this);

		/*
		var ps = this.toolboxPanes, tb = this.html.toolbox;
		tb.adopt(this.html.select);

		for (var c in ps) {
			tb.adopt(ps[c][1].html.main);

			var a = new Element('a', { href: '#' }),
				li = new Element('li', { 'class': c }),
				span = new Element('span', { text: ps[c][0] });

			a.adopt(span);
			li.adopt(a);

			this.html.select.adopt(li);

			a.addEvent('click', (function (e, which) {
				e.preventDefault();

				var cn = which + '-selected';
				this.set('class', 'toolbox' + (this.hasClass(cn) ? '' : ' ' + cn));
			}).bindWithEvent(this.html.toolbox, c));
		}

		this.html.main.adopt(this.html.toolbox);
		*/

		if (location.hash)
			this.load(location.hash.substring(1));
		else
			this.setGame(new Tack.Game());

		(function () {
			if (this.loading) return;

			var id = parseInt(location.hash.substring(1));
			if (id && this.game && id != this.game.id)
				this.load(id);
		}).periodical(500, this);

		this.html.main.adopt(this.sidebar.html.main);
		$(container || document.body).adopt(this.html.main);
	},

	load: function (id) {
		if (this.loading) return;

		this.loading = true;

		var t = this,
			loading = new Element('div', { id: 'loading' }).inject(document.body),
			req = new Request.JSON({url: 'server.php',
				onComplete: function (response) {
					loading.destroy();

					switch (response.result) {
						case 'error':
							location.hash = '#';
							alert(response.error);
							if (!t.game) t.setGame(new Tack.Game());
							break;
						case 'success':
							t.setGame(Tack.Game.decompile(response.game));
							t.game.id = response.gameId;
							location.hash = '#' + t.game.id;
							break;
					}
	
					t.loading = false;
					t.refresh();
				}}).post({ action: 'get', gameId: id });
	},

	refresh: function () {
		this.designer.refresh();

		//for (var i in this.toolboxPanes) {
		//	this.toolboxPanes[i][1].refresh();
		//}
		
		this.sidebar.refresh();
	},

	save: function (authKey) {
		var t = this,
			loading = new Element('div', { id: 'loading' }).inject(document.body),
			req = new Request.JSON({url: 'server.php',
				onComplete: function (response) {
					loading.destroy();

					switch (response.result) {
						case 'error':
							alert(response.error);
							break;
						case 'success':
							t.game.authKey = response.authKey;
							t.game.id = response.gameId;
							window.location.hash = '#' + response.gameId;
							break;
					}
	
					t.refresh();
				}}).post({ action: 'set', authKey: authKey, data: JSON.encode(t.game.compile()).replace(/\r\n|\r|\n/g, '\\n') });
	},

	setGame: function (game) {
		this.game = game;

		this.designer.setScene(game.startingScene);
		this.player.setGame(game);

		this.refresh();
		this.player.play();
	}
});
